﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_Menu_Menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        txtTenChuDe.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 

        if (!IsPostBack)
        {
             BindData();
        }
        else
        {
            if (hPccMsg.Value != "" && hPccMsg.Value != null)
            {
                string strXML = "<PccMsg>" + hPccMsg.Value + "</PccMsg>";
                PccMsg msg = new PccMsg(strXML);
                string Submit = msg.Query("Method");
                if (Submit == "Refesh")
                {
                    BindData();
                    //Response.Redirect(Request.UrlReferrer.AbsoluteUri.ToString());
                    hPccMsg.Value = "";

                }
            }
        }
    }
   
    protected void btnAdd_Catalogue_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddMenu.aspx?CodeID="+Request.QueryString["CodeID"] +"&sKindNo=" + ddlKindNo.SelectedValue);
    }

     protected DataTable  Bind_Edit_DropList_Menu()
     {
        clsWebShop cls = new clsWebShop();
        DataTable dt =new DataTable();
        dt = cls.GetData("select DISTINCT * from Menu order by MenuName").Tables[0];
        return dt;
     }
    private void BindData()
    {
        PccMsg msg = new PccMsg();
        string Groupid = "0";
        //ddlKindNo.SelectedItem.Selected = false;
        if (Request.QueryString["sKindNo"] != "" && Request.QueryString["sKindNo"]!=null)
            ddlKindNo.Items.FindByValue(Request.QueryString["sKindNo"]).Selected = true;

        if (Request.QueryString["Groupid"] != "" && Request.QueryString["Groupid"] != null)
        {
            ddlKindNo.Enabled = false;
            btnReturn.Visible = true;
            Groupid = Request.QueryString["Groupid"];
        }
        msg.CreateFirstNode("MenuName", txtTenChuDe.Text.Trim());
        msg.CreateFirstNode("LevelMenuID", Groupid);
        msg.CreateFirstNode("KindNo", ddlKindNo.SelectedValue);

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_MenuByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    protected void grv_Master_RowCreated(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < grv_Master.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.WebControl myControl = grv_Master.Rows[i].Cells[1];
            myControl.Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
        }


    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].ColumnSpan = 2;
            //e.Row.Cells.RemoveAt(2);
            e.Row.Cells.RemoveAt(1);
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                clsWebShop cls = new clsWebShop();
                PccMsg msg = new PccMsg();
                Label lblLevelMenu = (Label)e.Row.FindControl("lblLevelMenu");
                Label lblKindNo = (Label)e.Row.FindControl("lblKindNo");
                if (lblKindNo.Text == "A")
                {
                    lblKindNo.Text = "Admin";
                    if (Request.QueryString["Groupid"] == "" || Request.QueryString["Groupid"] == null)
                    {
                        Label lblMenuName = (Label)e.Row.FindControl("lblMenuName");
                        lblMenuName.Text = "<a href='Menu.aspx?CodeID="+Request.QueryString["CodeID"] +"&Groupid=" + DataBinder.Eval(e.Row.DataItem, "MenuID") + "' style='color:#121212'>" + lblMenuName.Text + "</a>";
                    }  

                }
                else
                    lblKindNo.Text = "Home";

                               
               
                msg.CreateFirstNode("MenuID", DataBinder.Eval(e.Row.DataItem, "LevelMenuID").ToString()); 
                DataTable dt = cls.Get_MenuByMenuID(msg.GetXmlStr.ToString()).Tables[0];
                if (dt.Rows.Count > 0)
                    lblLevelMenu.Text = dt.Rows[0]["MenuName"].ToString();
                else
                    lblLevelMenu.Text = "Nhóm menu";
            }
            else if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState)5)
            {
                DropDownList ddlLevelMenu = (DropDownList)e.Row.FindControl("ddlLevelMenu");
                ListItem item = new ListItem();
                item.Text = "--Nhóm menu--";
                item.Value = "0";
                ddlLevelMenu.Items.Insert(0, item);
                ddlLevelMenu.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "LevelMenuID").ToString()).Selected=true;

                DropDownList ddlEKindNo = (DropDownList)e.Row.FindControl("ddlEKindNo");
                ddlEKindNo.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "KindNo").ToString()).Selected = true;
                
            }
        }
        

       
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void grv_Master_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grv_Master.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void grv_Master_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grv_Master.EditIndex = -1;
        BindData();
    }
    protected void grv_Master_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      
        CheckBox chkEditChose = (CheckBox)grv_Master.Rows[e.RowIndex].FindControl("chkEditChose");
        TextBox txtETenMn= (TextBox)grv_Master.Rows[e.RowIndex].FindControl("txtETenMn");
        TextBox txtEMenuURL= (TextBox)grv_Master.Rows[e.RowIndex].FindControl("txtEMenuURL");
        DropDownList  ddlLevelMenu= (DropDownList)grv_Master.Rows[e.RowIndex].FindControl("ddlLevelMenu");
        DropDownList ddlEKindNo = (DropDownList)grv_Master.Rows[e.RowIndex].FindControl("ddlEKindNo");
        PccMsg msg = new PccMsg();
      
        msg.CreateFirstNode("MenuID", grv_Master.DataKeys[e.RowIndex].Value.ToString());
        msg.CreateFirstNode("MenuName", txtETenMn.Text.ToString());
        msg.CreateFirstNode("MenuURL", txtEMenuURL.Text.ToString().Trim());
        msg.CreateFirstNode("LevelMenuID", ddlLevelMenu.SelectedValue);
        msg.CreateFirstNode("KindNo", ddlEKindNo.SelectedValue);
        msg.CreateFirstNode("UpdID", "0");

        clsWebShop cls = new clsWebShop();
        cls.Upd_Menu(msg.GetXmlStr.ToString());


        //  ClientScript.RegisterClientScriptBlock(Me.GetType, "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        grv_Master.EditIndex = -1;
        BindData();
        //Response.Redirect(Request.UrlReferrer.AbsoluteUri.ToString());
    }
    protected void grv_Master_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("MenuID", grv_Master.DataKeys[e.RowIndex].Value.ToString());
        cls.Del_Menu(msg.GetXmlStr.ToString());
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BindData();
       // Response.Redirect(Request.UrlReferrer.AbsoluteUri.ToString());
    }
    private void fnUpd_Seq(string MenuID1,string MenuID2)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("MenuID1",MenuID1);
        msg.CreateFirstNode("MenuID2",MenuID2);
        cls.Upd_SeqMenu(msg.GetXmlStr.ToString());
        BindData();
        
    }
    protected void btnSearch_Click1(object sender, EventArgs e)
    {
        BindData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Menu.aspx?CodeID="+Request.QueryString["CodeID"]);
    }
    protected void ddlKindNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        BindData();
    }
}
